/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Controladores.Action;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionMessage;

import AccesoBaseDatos.GatewayPlanEstudio;
/**
 *
 * @author Franklin
 */
public class AgregarCursoPlanEstudioAction extends org.apache.struts.action.Action {

    /* tenemos una instacia del formbean para sacar los datos */
   private Controladores.Form.AgregarCursoPlanEstudioForm formulario;
   //esto son unas etiquetas que me sirven para redireccionar la pagina en la evaluacion de campos
   private final static String SUCCESS = "success";
   private final static String FAILURE = "failure";
   private final static String FALLACONEXION = "fallaConexion";
      private final static String NO = "noEsta";
   ActionForward retorno;

    @Override
    public ActionForward execute(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        
        formulario=(Controladores.Form.AgregarCursoPlanEstudioForm) form;
        //guarda la pagina para la que se va a redireccionar
        retorno = new ActionForward();
        boolean exito = false;
        if(comprobarCodigo(formulario.getID())||comprobarSigla(formulario.getSigla())||comprobarAS(formulario.getAno(),formulario.getSemestre())){
            //Seteo el error
            ActionErrors errors = new ActionErrors();
            errors.add("Problema", new ActionMessage("errors.noNulos"));
            //almaceno el error
            saveErrors(request, errors);
            retorno = mapping.findForward(FAILURE);       
        }else{//crea la conexion
           GatewayPlanEstudio gatewayPlanEstudio = new GatewayPlanEstudio();
           //ve que existan el plan de estudio y el curso
           if(gatewayPlanEstudio.VerificarExistenciaPlanCurso(formulario.getID(), formulario.getSigla())){ //actualizar
               //verifica si ya esta la tupla en la tabla de ciclo
               if(gatewayPlanEstudio.VerificaSiCursoEnPlan(formulario.getID(), formulario.getSigla())){
                   //exito = true;
                   exito = gatewayPlanEstudio.UpdatePlanEstudioCiclos(formulario.getID(), formulario.getSigla(), formulario.getAno(), formulario.getSemestre());
               }else{
                   //exito = true;
                   exito = gatewayPlanEstudio.InsertPlanEstudioCiclos(formulario.getID(), formulario.getSigla(), formulario.getAno(), formulario.getSemestre());
               }
               if(exito == true){
                   retorno = mapping.findForward(SUCCESS);
               }else{
                   retorno = mapping.findForward(FALLACONEXION);
               }
           }else{//insertar
               retorno = mapping.findForward(FALLACONEXION);
           }        
        }
        return retorno;//devuelve el lugar donde se debe devolver el control
    }
    
    public boolean comprobarCodigo(String codigo){
        boolean correcto = true;
        if(codigo.equals("")||codigo.length()!=5){
            correcto=true;
        }else{    
            correcto=false;
        }
        return correcto;
    }
    public boolean comprobarAS(String ano, String semestre){
        boolean correcto = true;
        if(ano.equals("")||semestre.equals("")||ano.length()!=1||semestre.length()!=1){
            correcto=true;
        }else{    
            correcto=false;
        }
        return correcto;
    }
    public boolean comprobarSigla(String codigo){
        boolean correcto = true;
        if(codigo.length()!=7||codigo.equals("")||codigo.charAt(2)!='-'){
            correcto=true;
        }else{    
            correcto=false;
        }
        return correcto;
    }
    
    
    
}
